using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace CoreApp.Utility
{
    public class CommonUnit
    {
        /// <summary>
        /// 获取程序集下的类及对应继承的接口的集合
        /// </summary>
        /// <param name="assemblyName">程序集</param>
        /// <param name="suffixes">类名的后缀</param>
        /// <returns></returns>
        public static Dictionary<Type, Type[]> GetClassAndInterfacePairList(string assemblyName, string nameSuffixes)
        {
            var result = new Dictionary<Type, Type[]>();

            if (!String.IsNullOrEmpty(assemblyName))
            {
                Assembly assembly = Assembly.Load(assemblyName);
                List<Type> typeList = assembly.GetTypes().ToList();
                List<Type> eligibleTypes = typeList.Where(x => !x.IsInterface && x.Name.EndsWith(nameSuffixes)).ToList();
                foreach (var item in eligibleTypes)
                {
                    var interfaceType = item.GetInterfaces();
                    result.Add(item, interfaceType);
                }
            }

            return result;
        }
    }
}